package lc20240613;

import java.util.ArrayList;
import java.util.List;

/**
  * @description 全排列
  * @author 不知名帅哥
  * @date 2024/6/13 22:20
  * @version 1.0
*/
public class FullPermutation {
    public static void main(String[] args) {

        FullPermutation permutation=new FullPermutation();
        int[] nums={1,2,3};
        List<List<Integer>> permute = permutation.permute(nums);
        System.out.println("permute = " + permute);
    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> cur=new ArrayList<>();
        if (nums.length == 0) {
            return res;
        }
        backTrack(nums, 0, res,cur);
        return res;
    }

    public void backTrack(int[] nums,int start, List<List<Integer>> res, List<Integer> curList) {
        if (start == nums.length) {
            res.add(new ArrayList<>(curList));
            return;
        }
        for (int i = start; i < nums.length; i++) {
            swap(nums,i,start);
            curList.add(nums[start]);
            backTrack(nums,start+1,res,curList);
            //移除当前排列的最后一个元素
            curList.remove(curList.size()-1);
            // 恢复交换，撤销上一步操作
            swap(nums,i,start);
        }
    }
    private void swap(int[] nums,int i,int start){
        int temp=nums[i];
        nums[i]=nums[start];
        nums[start]=temp;
    }
}
